/******************************************************************************************************
Copyright (C) 2019 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/

/*globals Molecule, Atom, Str, mainWindow, print, MolecularAssignmentsParser*/
/*jslint plusplus: true, indent: 4*/


function testMolecularAssignmentsParser() {
	"use strict";

	var molecule, assignmentParser, i, j, k, assign, notAtom, atom,
		elements = [ "C", "H", "N", "F"],
		shiftTypes = ["Peak", "Multiplet", "Integral"],
		labels = [],
		uuids = [],
		atoms = [],
		shifts = [],
		doc = mainWindow.activeDocument;

	function printFormatted(aObject) {
		print(JSON.stringify(aObject, null, 4) + "\n\n");
	}

	molecule = new Molecule(doc.item(0, "Molecule"));
	if (molecule && molecule.isValid()) {
		assignmentParser = new MolecularAssignmentsParser(molecule);
	} else {
		return;
	}

	for (i = 1; i < 4; i++) { //to collect uuids from first 3 atoms
		assign = assignmentParser.getFromAtomIndex(i);
		for (j = 0; j < assign.length; j++) {
			if (assign[j].shift[0].idTypes && assign[j].shift[0].idTypes.length) {
				for (k = 0; k < assign[j].shift[0].idTypes.length; k++) {
					uuids.push(assign[j].shift[0].idTypes[k].uuid);
				}
			}
		}
	}
	for (i = 0; i < 3; i++) { //to collect first 3 real labels, directly from assignments array
		labels.push(assignmentParser.assignments[i].atom.label);
	}
	for (i = 0; i < 3; i++) { //to collect first 3 atoms, directly from assignments array
		atom = {};
		atom.index = assignmentParser.assignments[i].atom.index;
		if (assignmentParser.assignments[i].atom.h) {
			atom.h = assignmentParser.assignments[i].atom.h; 
		}
		atoms.push(atom);
	}
	for (i = 0; i < 3; i++) { //to collect first 3 shifts, directly from assignments array
		shifts.push(assignmentParser.assignments[i].shift[0].shift);
	}		

	print("All asignments:\n");
	printFormatted(assignmentParser.getAll());

	//test index
	for (i = 1; i < 4; i++) {
		print("From atom index " + i + ":\n");
		printFormatted(assignmentParser.getFromAtomIndex(i));
	}
	print("From non existing atom index 10000:\n");
	printFormatted(assignmentParser.getFromAtomIndex(10000));


	//test element
	for (i = 0; i < elements.length; i++) {
		print("From element " + elements[i] + ":\n");
		printFormatted(assignmentParser.getFromAtomElement(elements[i]));
	}
	print("From non existing element UNKNOWN:\n");
	printFormatted(assignmentParser.getFromAtomElement("UNKNOWN"));


	//test type
	for (i = 0; i < shiftTypes.length; i++) {
		print("From shift type " + shiftTypes[i] + ":\n");
		printFormatted(assignmentParser.getFromShiftType(shiftTypes[i]));
	}
	print("From non existing shift type UNKNOWN:\n");
	printFormatted(assignmentParser.getFromShiftType("UNKNOWN"));


	//test label
	for (i = 0; i < labels.length; i++) {
		print("From label " + labels[i] + ":\n");
		printFormatted(assignmentParser.getFromAtomLabel(labels[i]));
	}
	print("From non existing label UNKNOWN:\n");
	printFormatted(assignmentParser.getFromAtomLabel("UNKNOWN"));


	//test uuid
	for (i = 0; i < uuids.length; i++) {
		print("From peak/multiplet/integral uuid " + uuids[i] + ":\n");
		printFormatted(assignmentParser.getFromShiftUUID(uuids[i]));
	}
	print("From non existing peak/multiplet/integral uuid UNKNOWN:\n");
	printFormatted(assignmentParser.getFromShiftUUID("UNKNOWN"));


	//test Atom
	for (i = 0; i < atoms.length; i++) {
		print("From atom " + JSON.stringify(atoms[i]) + ":\n");
		printFormatted(assignmentParser.getFromAtom(atoms[i]));
	}
	notAtom = {"index" : 10000};
	print("From non existing atom :" + JSON.stringify(notAtom) + ":\n");
	printFormatted(assignmentParser.getFromAtom(notAtom));


	//test Shift
	print("Note that getting atoms from shifts is less accurate, due to spectral overlap and shift variation due to experimental conditions\n\n");	
	
	for (i = 0; i < atoms.length; i++) {
		print("From shift " + shifts[i] + " (will get all atoms with shifts within a range of 0.05ppm):\n");
		assignFromShift = assignmentParser.getFromShift(shifts[i]);
		printFormatted(assignFromShift);
		print("From shift " + shifts[i] +  " using the previous output as input, to refine search (starting from a subset will get all atoms with shifts within a range of 0.01ppm):\n");
		printFormatted(assignmentParser.getFromShift(shifts[i], assignFromShift));
	}

	notShift = 10000;
	print("From non existing shift " + notShift + ":\n");
	printFormatted(assignmentParser.getFromShift(notShift));
}

